package com.clusterswisdom.horn.api.tmall.action.dataSyn;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.clusterswisdom.horn.api.ioc.HornAction;
import com.clusterswisdom.horn.api.ioc.HornResponse;
import com.clusterswisdom.horn.api.ioc.api.ApiConfig;
import com.clusterswisdom.horn.api.tmall.dataSynService.DbSynHandleTo;
import com.clusterswisdom.horn.api.tmall.dataSynService.common.DbSynConstrant;
import com.clusterswisdom.horn.api.tmall.dataSynService.imp.TargetDbService;

/**
 * 获取同步数据action
 * @author liqingsong
 *
 */
public class receiveDataToDbAction implements HornAction {
 
//	private int i =0;
	 
	public HornResponse execute(ApiConfig config, JSONObject param) throws Exception {
//		i++;
//		System.out.println(i);
		try
		{
		
			return saveDateToDb(param);
			 
		}catch(Exception e)
		{
			return new HornResponse(false, e.getMessage()); 
		}
		
		
		
	}
	
	
	private HornResponse saveDateToDb(JSONObject param)
	{

		//源库端发送的同步数据
		//JSONObject receiveDates =  param.getJSONObject("bizData").getJSONObject("sendDatas");
		JSONObject receiveDates =  param.getJSONObject("sendDatas");
		
		DbSynHandleTo handle = new TargetDbService();
		HornResponse response = new HornResponse(true, "");
		Map<String,Object> result = new HashMap<String,Object>();
		
		try
		{
			//根据发送的同步数据，将数据存入本地库
			result = handle.toDbDate(receiveDates);
		    String resultCode =(String) result.get(DbSynConstrant.ACK_RESULTCODE);
		    if(DbSynConstrant.ACKCODE_FAILE.equals(resultCode))
		    {
		    	response = new HornResponse(false, result);
		    }
		}catch( Exception e)
		{
			e.printStackTrace();
			response = new HornResponse(false,e.getMessage()); 		
		}finally
		{
			handle =null;	
		}
		
		return response;
	}

}
